import { message } from 'antd';
import { classTableInfoList,fetchMembersList,getMemberCard,queryMemberBookingCourseInfoByClassId, fetchShopList,fetchCoach,queryClassTableInfoById,fetchTimeList,querySiteInfoListByVenueIdNotPage,queryUsableSiteInfoList} from './service';

const Model = {
  namespace: 'cubeReservation',
  state: {
    classTableInfoList:[],
    memberBookInfoList:[],
    membersList:[],
    membersCardList:[],
    member:null,
    memberCourseInfoList:[],
    shopList:[],
    coachList:[],
    classTableInfo:{},
    timeList:[],
    siteList:[],
    leftSiteList:[]
  },
  effects: {
    *classTableInfoList({ payload }, { call,put }) {
      let result=yield call(classTableInfoList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            classTableInfoList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *queryMemberBookingCourseInfoByClassId({ payload }, { call,put }) {
      if(!payload.classId) return;
      let result=yield call(queryMemberBookingCourseInfoByClassId, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            memberBookInfoList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchMembersList({ payload }, { call,put }) {
      let result=yield call(fetchMembersList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            membersList:result.data          }
        });
      }else{
        message.error(result.message);
      }
      return result?.data
    },
    *getMemberCard({ payload }, { call,put }) {
      let result=yield call(getMemberCard, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            membersCardList:result.data       
        }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchShopList({ payload }, { call,put }) {
      let result=yield call(fetchShopList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            shopList:result.data          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchCoach({ payload }, { call,put }) {
      let result=yield call(fetchCoach, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            coachList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *queryClassTableInfoById({ payload }, { call,put }) {
      let result=yield call(queryClassTableInfoById, payload);
      if(result.code===0){
        if(Array.isArray(result.data)&&result.data.length>0){
          yield put({
            type: 'common',
            payload: {
              classTableInfo:result.data[0]
            }
          });
        }
      }else{
        message.error(result.message);
      }
    },
    *fetchTimeList({ payload }, { call,put }) {
      let result=yield call(fetchTimeList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            timeList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *querySiteInfoListByVenueIdNotPage({ payload }, { call,put }) {
      let result=yield call(querySiteInfoListByVenueIdNotPage, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            siteList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *querySiteInfoLeftList({ payload }, { call,put }) {
      let result=yield call(queryUsableSiteInfoList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            leftSiteList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *reset({ payload }, { call,put }) {
      yield put({
        type: 'common',
        payload: {
          membersList:[],
          membersCardList:[],
          member:null,
        }
      });
    },
  },
  reducers: {
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
